package com.zrrd.learn.chapter05;

/**
 * 数组
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/11 11:12
 */

public class LearnArray02 {

    public static boolean question1(int x, int[] arr) {

        boolean ist = false;
        /**
         * 1 : 对数组进行一个一个的查看 （循环）
         * 2 : 是否应该对取出来的每一个数组 进行比较
         * 3 : 把结果ist
         */
        for (int i = 0, max = arr.length; i < max; i++) {
            if (arr[i] == x) {
                ist = true;
                break;
            }
        }
        return ist;

    }

    /**
     * 用来判断arr1是否完全包含在arr2里面，如果存在返回true  反之返回false
     * @param arr1  子数组
     * @param arr2  目标数组
     * @return      是否存在
     */
    public static boolean question2(int[] arr1, int[] arr2) {

        boolean ist = false;

        for (int i = 0, max = arr2.length; i < max; i++) {
            if (arr2[i] == arr1[0]) {
                ist = true;
                break;
            }
        }
        return ist;

    }

    /**
     * 用来判断sub是否完全包含在target里面，如果存在返回true  反之返回false
     * @param sub  子数组
     * @param target  目标数组
     * @return      是否存在
     */
    public static boolean question2_1(int[] sub, int[] target) {
        boolean t = true;
        /**
         * 1 : 容错处理,判断一下target.length一定大于sub.length
         * 1.1  初始位置 的判断
         * 2 :  循环
         */
        if (target.length < sub.length) {
            return false;
        }

        L:for (int i = 0; i < target.length; i++) {
            //i = 3  target[3] == sub[0]
            if (target[i] == sub[0]) {
                for (int j = 1; j < sub.length; j++) {
                    if (target[i + j] != sub[j]) {

                        break L;
                    }
                }
            }
        }

        return t;
    }

    public static boolean question2_2(int[] sub, int[] target) {
        /**
         * 1 : 容错处理,判断一下target.length一定大于sub.length
         * 1.1  初始位置 的判断
         * 2 :  循环
         */
        if (target.length < sub.length) {
            return false;
        }

        for (int i = 0; i < target.length; i++) {
            int num = 0;
            for (int j = 0; j < sub.length; j++) {
                //容错
                if(i + j < target.length){
                    //比较
                    if (sub[j] == target[i + j]) {
                        num++;
                    }
                }
            }
            //是否结束
            if(num == sub.length){
                return true;
            }
        }
        return false;
    }


    public static boolean question3(int[] arr3, int[] arr4) {

        boolean ist = false;
        /**
         * 1 : 对数组进行一个一个的查看 （循环）
         * 2 : 是否应该对取出来的每一个数组 进行比较
         * 3 : 把结果ist
         */
        for (int i = 0, max = arr4.length; i < max; i++) {
            if (arr4[i] == arr3[0] && arr4[i+1] == arr3[1]) {
                ist = true;
                break;
            }
        }
        return ist;

    }

    //4 : a : [1，2,3,4] b : [2,3,4,5]  左差集 以左而的为主 a为主 找a里面不在b里面
    //    右
    //5 : a : {1,1,1,2,3,4,5,1,2,3,4,7,6} 每一个元素出现的次数 (只有一次的数)


    public static boolean question3_1(int[] sub, int[] target) {
        /**
         * 1 : 容错处理,判断一下target.length一定大于sub.length
         * 1.1  初始位置 的判断
         * 2 :  循环
         */
        if (target.length < sub.length) {
            return false;
        }
        int num = 0;
        L: for (int i = 0; i < target.length; i++) {
            for (int j = 0; j < sub.length; j++) {
                //容错
                if(sub[i] == target[j]){
                    num++;
                   if(num == sub.length){
                      return true;
                   }
                   break L;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    public static void question4(int[] arr5, int[] arr6) {
        System.out.printf("4题\n");
        for (int i = 0; i < arr5.length; i++) {
            if(!question1(arr5[i],arr6)){
                System.out.printf("左差集"+arr5[i]+"\n");
            }
        }
        for (int i = 0; i < arr6.length; i++) {
            if(!question1(arr6[i],arr5)){
                System.out.printf("右差集"+arr6[i]+"\n");
            }
        }
        System.out.printf("全差集");
        for (int i = 0; i < arr5.length; i++) {
            if(!question1(arr5[i],arr6)){
                System.out.printf(""+arr5[i]);
            }
        }
        for (int i = 0; i < arr6.length; i++) {
            if(!question1(arr6[i],arr5)){
                System.out.printf("\t"+arr6[i]);
            }
        }


    }


    public static void question5(int[] arrc) {
        System.out.printf("\n5题\n");
        int[] arrx;
        int num=0;
        L: for (int i = 0; i < arrc.length; i++) {
            if(question1(arrc[i],arrc)){
                num++;if(num==1){
                    System.out.printf("?"+arrc[i]+"\n");
                }
//                break L;
            }

        }



    }

    public static void main(String[] args) {
        int x = 3;
        int[] arr = new int[4];
        arr[0] = 3;
        arr[1] = 4;
        arr[2] = 6;
        arr[3] = 16;
        System.out.printf("1题");
        System.out.println(question1(x, arr));

        int[] arr1 = new int[1];
        arr1[0] = 5;
        int[] arr2 = new int[4];
        arr2[0] = 1;
        arr2[1] = 2;
        arr2[2] = 3;
        arr2[3] = 4;
        System.out.printf("2题");
        System.out.println(question2(arr1, arr2));

        int[] sub = new int[3];
        sub[0] = 1;
        sub[1] = 2;
        sub[2] = 3;
        int[] target = new int[7];
        target[0] = 1;
        target[1] = 2;
        target[2] = 3;
        target[3] = 4;
        target[4] = 1;
        target[5] = 2;
        target[6] = 1;
        System.out.printf("2题-1");
        System.out.println(question2_1(sub, target));

        int[] a = {1,2,3};
        int[] b = {1,2,3,4,1,2};
        System.out.printf("2题-2");
        System.out.println(question2_2(a, b));




        int[] arr3 = new int[2];
        arr3[0] = 1;
        arr3[1] = 4;
        int[] arr4 = new int[4];
        arr4[0] = 1;
        arr4[1] = 4;
        arr4[2] = 3;
        arr4[3] = 4;
        System.out.printf("3题");
        System.out.println(question3(arr3, arr4));

        int[] e = {1,2};
        int[] f = {1,2,3,4,9};
        System.out.printf("3题-1");
        System.out.println(question3_1(e, f));

        int[] arr5 = {1,2,3,4};
        int[] arr6 = {2,3,4,5};
        question4(arr5,arr6);

        int[] arrc = {1,1,1,1,2,2,3,4};
        question5(arrc);
    }









}
